package com.example.prison.mapper;

import com.example.prison.entity.UserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【user_info】的数据库操作Mapper
* @createDate 2023-05-26 11:28:10
* @Entity com.example.prison.entity.UserInfo
*/
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

    @Select("<script>select * from user_info " +
            " <where> " +
            " <if test=\"userName != null and userName != ''\">and user_name like concat('%',#{userName},'%') </if> " +
            " </where> " +
            "order by user_id desc</script>")
    List<UserInfo> get(@Param("userName") String userName);

    @Insert("insert into user_info(user_name, password, department, role_name, function) " +
            "values (#{userName}, #{password}, #{department}, #{roleName}, #{function})")
    int add(UserInfo user);

    @Delete("<script>delete from user_info where user_id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void deleteUser(@Param("ids") Integer[] ids);

//    @Update("update user_info set user_name=#{user.userName}, password=#{user.password}, department=#{user.department}, " +
//            "role_name=#{user.roleName}, function=#{user.function} where user_id = #{id}")
    @Update({"<script> UPDATE user_info",
            "<set>",
            "<if test='user.userName != null'>user_name = #{user.userName},</if>",
            "<if test='user.password != null'>password = #{user.password},</if>",
            "<if test='user.department != null'>department = #{user.department},</if>",
            "<if test='user.roleName != null'>role_name = #{user.roleName},</if>",
            "<if test='user.function != null'>function = #{user.function},</if>",
            "</set>",
            "where user_id = #{id} </script>"})
    int updateUser(UserInfo user,Integer id);

    @Update("update user_info set user_name=#{user.userName}, password=#{user.password}, department=#{user.department}, role_name=#{user.roleName}, function=#{user.function} where user_id = #{id}")
    int updateUserFail(UserInfo user,Integer id);

    @Select("select * from user_info where user_name like concat('%',#{name},'%') order by user_id desc")
    List<UserInfo> getByName(@Param("name") String name);

    @Select("select * from user_info where user_name=#{name} order by user_id desc")
    List<UserInfo> getBySameName(@Param("name") String name);

    @Select("select * from user_info where user_name=#{name}")
    UserInfo getOneByName(@Param("name") String name);

    @Select("select * from user_info where user_id=#{id}")
    UserInfo getById(@Param("id") Integer id);

}




